
<%@ tag language="java" pageEncoding="UTF-8"
	trimDirectiveWhitespaces="true"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="isperp" uri="/WEB-INF/tlds/isperp.tld"%>

<%@ attribute name="gridId" required="true" rtexprvalue="true"
	type="java.lang.String" description=""%>

<%@ attribute name="id" required="true" rtexprvalue="true"
	type="java.lang.String" description=""%>

<%@ attribute name="vid" required="true" rtexprvalue="true"
	type="java.lang.String" description=""%>

<%@ attribute name="sentType" required="true" rtexprvalue="true"
	type="java.lang.String" description=""%>
	
<%@ attribute name="readOnly" required="false" rtexprvalue="true"
	type="java.lang.String" description=""%>

<script type="text/javascript">
function ${gridId}addMailTo() {
		isperp.openDialog({
			id : "${id}Dialog",
			onThisWindow : true,
			contentUrl : 'change/elementRelation/changeAddSentToEdit',
			title : "添加发送对象",
			windowSize:"small",
			buttons : [ {
				text : "保存并添加",
				"class" : 'nui-mainbtn',
				click : function() {
					var winObj = $("#${id}Dialog").find("iframe")[0].contentWindow;
					${gridId}addMailFun(winObj, "${gridId}", "${sentType}");
					winObj.document.getElementById("toName").value = "";
					winObj.document.getElementById("toId").value = "";
					return false;
				}
			}, {
				text : "保存",
				"class" : 'grey',
				click : function() {
					var winObj = $("#${id}Dialog").find("iframe")[0].contentWindow;
					${gridId}addMailFun(winObj, "${gridId}", "${sentType}");
					$(this).wijdialog("close");
				}
			}, {
				text : "关闭",
				"class" : 'grey',
				click : function() {
					$(this).wijdialog("close");
				}
			} ]
		});
		return false;
	}
	
	function ${gridId}addMailFun(winObj, gridId, type) {
		//接收人、接收地址
		var toName = winObj.document.getElementById("toName").value;
		if (toName == "") {
			isperp.alert("请输入发送对象名称");
			return false;
		}
		var toId = winObj.document.getElementById("toId").value;
		if (toId == "") {
			isperp.alert("请输入接收地址");
			return false;
		} else {
               if(!isEmail(toId))
               {
                  isperp.alert("请输入正确的邮箱地址");
                  return false;
               }
		}
		var fromId = '${model.id}';
		${gridId}addChgElementRelation(fromId, toId, type, toName, gridId);

	}

	function isEmail(str) {
		var reg = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/ ;
		return reg.test(str);
	}

	function ${gridId}addChgElementRelation(fromId, toId, type, name, gridId) {
		$.ajax({
			url : "change/elementRelation/saveChgElementRelation?from_id=" + fromId + "&name=" + name + "&ty=" + type + "&to_id=" + toId,
			success : function(data) {
				$("#" + gridId).trigger("reloadGrid");
			}
		});
	}

	function ${gridId}selectSentToPerson(row) {
		var id = '${model.id}';
		var names = row[0];
		$.ajax({
			url : "change/elementRelation/insertMailToByUserName?id=" + id + "&names=" + names + "&ty=${sentType}",
			success : function(data) {
				${gridId}.search();
			}
		});
	}

</script>

<c:choose>
  <c:when test="${readOnly eq true }">
     <label id="${gridId }MailList">
        
     </label>
     <script type="text/javascript">
        $(function(){
           var id='${model.id}';
           $.ajax({
			url : "change/elementRelation/getOneFromViewRelation?id=" + id + "&ty=${sentType}",
			success : function(data) {
				$("#${gridId }MailList").text(data.mail_format);
			}
		});
        });
     </script>
  </c:when>
  <c:otherwise>
     <isperp:grid
	url="change/elementRelation/listByFilter?id=${model.id }&ty=${sentType }"
	gridId="${gridId }" gridTitle="接收人" cusDialogSize="normal"
	cusFormatterActions="true" rowNum="5"
	cusDeleteUrl="change/elementRelation/delete" showPager="false"
	height="145" showSearchbar="false">
	<jsp:attribute name="toolbar">
	        <button class="nui-mainbtn" onclick="${gridId }addMailTo();return false;" id="addOther${gridId }">添加</button>
			<button class="nui-mainbtn"
			onclick="javascript:$('#${id }${gridId }').click();return false;">选择接收人员</button>
			</jsp:attribute>
	<jsp:body>
					<isperp:column name="id" title="id" hidden="true"></isperp:column>
					<isperp:column name="name" title="接收人" width="258"></isperp:column>
			        <isperp:column name="toId" title="邮箱地址" width="458">
					</isperp:column>
				</jsp:body>
</isperp:grid>
<div style="display:none">
	<input id="${vid }">
	<isperp:departmentUser displayValue="" ifIncludeNoRoleUser="true"
		afterSelected="${gridId }selectSentToPerson(row);" multiselect="true"
		displayValueStyle="width:249px;" displayValueCssClass="required"
		idFieldName="name" valueControlId="${vid }" id="${id }${gridId }" name="${id }${gridId }"></isperp:departmentUser>
</div>
  </c:otherwise>
</c:choose>





